﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EdgeStatz.ESSystems.Objects;

namespace EdgeStatz
{
    public partial class GetProviders : Form
    {
        List<Provider> providers = new List<Provider> { };
        Provider selection = null;
        public string Message
        { set { label2.Text = value; } }
        
        public Provider[] Providers
        {
            get { return providers.ToArray(); }
            set { providers = new List<Provider> { }; providers.AddRange(value); RefreshProviders(); }
        }

        public Provider Selection
        {
            get { return selection; }
        }

        public GetProviders(Provider[] providers)
        {
            InitializeComponent();
            this.providers.AddRange(providers);
            RefreshProviders();
        }

        private void RefreshProviders()
        {
            listBox1.Items.Clear();
            foreach (Provider prov in providers)
                listBox1.Items.Add(prov.Name);
        }

        private void b_Add_Click(object sender, EventArgs e)
        {
            EditProvider newProv = new EditProvider();
            if (newProv.ShowDialog() == DialogResult.OK)
            {
                Provider prov = newProv.Provider;
                if (prov.IsDefault)
                    foreach (Provider pr in providers)
                        pr.IsDefault = false;
                providers.Add(prov);
                RefreshProviders();
                listBox1.SelectedItem = prov.Name;
            }
        }

        private void b_Edit_Click(object sender, EventArgs e)
        {
            if(listBox1.SelectedItem == null)
            { MessageBox.Show("Select a provider to edit."); return; }
            Provider old_prov = null;
            foreach (Provider pr in providers)
                if (pr.Name.Equals(listBox1.SelectedItem.ToString()))
                    old_prov = pr;

            EditProvider newProv = new EditProvider(old_prov);
            if (newProv.ShowDialog() == DialogResult.OK)
            {
                Provider prov = newProv.Provider;
                if (prov.IsDefault)
                    foreach (Provider pr in providers)
                        pr.IsDefault = false;
                int index = providers.IndexOf(old_prov);
                providers.RemoveAt(index);
                providers.Insert(index, prov);
                RefreshProviders();
                listBox1.SelectedItem = prov.Name;
            }
        }

        private void b_Delete_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            { MessageBox.Show("Select a provider to delete."); return; }

            Provider old_prov = null;
            foreach (Provider pr in providers)
                if (pr.Name.Equals(listBox1.SelectedItem.ToString()))
                    old_prov = pr;
            if (old_prov != null)
            {
                if (MessageBox.Show("Confirm Delete", "Are you sure you want to delete the provider {" + old_prov.Name + "}?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    providers.Remove(old_prov);
                    RefreshProviders();
                }
            }
        }

        private void b_OK_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedItem == null)
            { MessageBox.Show("Select a provider."); return; }
            
            Provider old_prov = null;
            foreach (Provider pr in providers)
                if (pr.Name.Equals(listBox1.SelectedItem.ToString()))
                    old_prov = pr;
            if (old_prov != null)
            {
                selection = old_prov;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void b_Close_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Abort;
            this.Close();
        }
    }

    class EditProvider : Form
    {
        Provider provider;
        public Provider Provider
        {
            get { return this.provider; }
            set 
            { 
                this.provider = value;
                tb_Name.Text = this.provider.Name;
                tb_Server.Text = this.provider.Server;
                tb_UserId.Text = this.provider.UserId ?? "";
                tb_Password.Text = this.provider.Password ?? "";
                cb_Default.Checked = this.provider.IsDefault;
            }
        }

        public EditProvider()
        {
            InitializeComponent();
        }
        public EditProvider(Provider provider) : this()
        {
            if (provider != null)
                this.Provider = provider;
        }

        private void b_Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void b_Ok_Click(object sender, EventArgs e)
        {
            if (tb_Name.Text.Trim().Equals(""))
            { MessageBox.Show("The name of the provider cannot be empty."); return; }
            if (tb_Server.Text.Trim().Equals(""))
            { MessageBox.Show("The name of the server cannot be empty."); return; }
            if (tb_UserId.Text.Trim().Equals(""))
                provider = new Provider(tb_Name.Text, tb_Server.Text, "", cb_Default.Checked);
            else
                provider = new Provider(tb_Name.Text, tb_Server.Text, "", tb_UserId.Text, tb_Password.Text, cb_Default.Checked);
            this.DialogResult = DialogResult.OK;
            this.Close(); ;
        }

        #region designer.cs
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.lbl_Name = new System.Windows.Forms.Label();
            this.lbl_Server = new System.Windows.Forms.Label();
            this.tb_Name = new System.Windows.Forms.TextBox();
            this.tb_Server = new System.Windows.Forms.TextBox();
            this.tb_UserId = new System.Windows.Forms.TextBox();
            this.tb_Password = new System.Windows.Forms.TextBox();
            this.cb_Default = new System.Windows.Forms.CheckBox();
            this.lbl_UserId = new System.Windows.Forms.Label();
            this.lbl_Password = new System.Windows.Forms.Label();
            this.b_Cancel = new System.Windows.Forms.Button();
            this.b_Ok = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lbl_Name
            // 
            this.lbl_Name.AutoSize = true;
            this.lbl_Name.Location = new System.Drawing.Point(12, 9);
            this.lbl_Name.Name = "lbl_Name";
            this.lbl_Name.Size = new System.Drawing.Size(41, 13);
            this.lbl_Name.TabIndex = 0;
            this.lbl_Name.Text = "Name: ";
            // 
            // lbl_Server
            // 
            this.lbl_Server.AutoSize = true;
            this.lbl_Server.Location = new System.Drawing.Point(12, 35);
            this.lbl_Server.Name = "lbl_Server";
            this.lbl_Server.Size = new System.Drawing.Size(44, 13);
            this.lbl_Server.TabIndex = 1;
            this.lbl_Server.Text = "Server: ";
            // 
            // tb_Name
            // 
            this.tb_Name.Location = new System.Drawing.Point(92, 6);
            this.tb_Name.Name = "tb_Name";
            this.tb_Name.Size = new System.Drawing.Size(228, 20);
            this.tb_Name.TabIndex = 2;
            // 
            // tb_Server
            // 
            this.tb_Server.Location = new System.Drawing.Point(92, 32);
            this.tb_Server.Name = "tb_Server";
            this.tb_Server.Size = new System.Drawing.Size(228, 20);
            this.tb_Server.TabIndex = 3;
            // 
            // tb_UserId
            // 
            this.tb_UserId.Location = new System.Drawing.Point(92, 58);
            this.tb_UserId.Name = "tb_UserId";
            this.tb_UserId.Size = new System.Drawing.Size(228, 20);
            this.tb_UserId.TabIndex = 4;
            // 
            // tb_Password
            // 
            this.tb_Password.Location = new System.Drawing.Point(92, 84);
            this.tb_Password.Name = "tb_Password";
            this.tb_Password.Size = new System.Drawing.Size(228, 20);
            this.tb_Password.TabIndex = 5;
            this.tb_Password.UseSystemPasswordChar = true;
            // 
            // cb_Default
            // 
            this.cb_Default.AutoSize = true;
            this.cb_Default.Location = new System.Drawing.Point(92, 110);
            this.cb_Default.Name = "cb_Default";
            this.cb_Default.Size = new System.Drawing.Size(60, 17);
            this.cb_Default.TabIndex = 6;
            this.cb_Default.Text = "Default";
            this.cb_Default.UseVisualStyleBackColor = true;
            // 
            // lbl_UserId
            // 
            this.lbl_UserId.AutoSize = true;
            this.lbl_UserId.Location = new System.Drawing.Point(12, 61);
            this.lbl_UserId.Name = "lbl_UserId";
            this.lbl_UserId.Size = new System.Drawing.Size(44, 13);
            this.lbl_UserId.TabIndex = 7;
            this.lbl_UserId.Text = "User Id:";
            // 
            // lbl_Password
            // 
            this.lbl_Password.AutoSize = true;
            this.lbl_Password.Location = new System.Drawing.Point(12, 87);
            this.lbl_Password.Name = "lbl_Password";
            this.lbl_Password.Size = new System.Drawing.Size(59, 13);
            this.lbl_Password.TabIndex = 8;
            this.lbl_Password.Text = "Password: ";
            // 
            // b_Cancel
            // 
            this.b_Cancel.Location = new System.Drawing.Point(245, 142);
            this.b_Cancel.Name = "b_Cancel";
            this.b_Cancel.Size = new System.Drawing.Size(75, 23);
            this.b_Cancel.TabIndex = 9;
            this.b_Cancel.Text = "Cancel";
            this.b_Cancel.UseVisualStyleBackColor = true;
            this.b_Cancel.Click += new System.EventHandler(this.b_Cancel_Click);
            // 
            // b_Ok
            // 
            this.b_Ok.Location = new System.Drawing.Point(164, 142);
            this.b_Ok.Name = "b_Ok";
            this.b_Ok.Size = new System.Drawing.Size(75, 23);
            this.b_Ok.TabIndex = 10;
            this.b_Ok.Text = "OK";
            this.b_Ok.UseVisualStyleBackColor = true;
            this.b_Ok.Click += new System.EventHandler(this.b_Ok_Click);
            // 
            // EditProvider
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(332, 177);
            this.ControlBox = false;
            this.Controls.Add(this.b_Ok);
            this.Controls.Add(this.b_Cancel);
            this.Controls.Add(this.lbl_Password);
            this.Controls.Add(this.lbl_UserId);
            this.Controls.Add(this.cb_Default);
            this.Controls.Add(this.tb_Password);
            this.Controls.Add(this.tb_UserId);
            this.Controls.Add(this.tb_Server);
            this.Controls.Add(this.tb_Name);
            this.Controls.Add(this.lbl_Server);
            this.Controls.Add(this.lbl_Name);
            this.Name = "EditProvider";
            this.Text = "EditProvider";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lbl_Name;
        private System.Windows.Forms.Label lbl_Server;
        private System.Windows.Forms.TextBox tb_Name;
        private System.Windows.Forms.TextBox tb_Server;
        private System.Windows.Forms.TextBox tb_UserId;
        private System.Windows.Forms.TextBox tb_Password;
        private System.Windows.Forms.CheckBox cb_Default;
        private System.Windows.Forms.Label lbl_UserId;
        private System.Windows.Forms.Label lbl_Password;
        private System.Windows.Forms.Button b_Cancel;
        private System.Windows.Forms.Button b_Ok;
        #endregion
    }
}
